package com.hn;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.hn.Entry.ServerOrderEntry;
import com.hn.listener.CatEventHandler;
import com.hn.Entry.BaiDuEntry;
import com.hn.common.ShareData;

import com.hn.utils.file.FileUtil;
import com.hn.utils.http.HttpUtil;
import com.hn.utils.timing.TimingUtil;
import net.mamoe.mirai.Bot;
import net.mamoe.mirai.BotFactory;
import net.mamoe.mirai.auth.BotAuthorization;
import net.mamoe.mirai.console.plugin.jvm.JavaPlugin;
import net.mamoe.mirai.console.plugin.jvm.JvmPluginDescriptionBuilder;
import net.mamoe.mirai.event.GlobalEventChannel;
import net.mamoe.mirai.utils.BotConfiguration;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.*;

public final class Cat extends JavaPlugin {
    public static final Cat INSTANCE = new Cat();
    private Cat() {
        super(new JvmPluginDescriptionBuilder(
                "com.hn.cat", "1.0.0")
                .name("cat")
                .author("Summer")
                .build());
    }
    @Override
    public void onEnable() {
        //初始化插件
        CatInit();
//        Bot bot = BotFactory.INSTANCE.newBot(3815829425L, BotAuthorization.byQRCode(), configuration -> {
//            configuration.setProtocol(BotConfiguration.MiraiProtocol.ANDROID_WATCH);
//        });
//        bot.login();
        //创建监听器
        GlobalEventChannel.INSTANCE.registerListenerHost(new CatEventHandler());
    }

     @SuppressWarnings("unchecked")
    public void CatInit(){
        try {
            //配置文件
            File MapFile = resolveConfigFile("marry.properties");
            if (!MapFile.exists()) {
                MapFile.createNewFile();
            }
            //初始化全局配置类
            ShareData instance = ShareData.getInstance();
            //创建定时器
            TimingUtil timingUtil = new TimingUtil();
            //设置一个每天凌晨0点执行的定时任务
            timingUtil.startTimedTasks(0, 0, 0, MapFile);
            timingUtil.closeOpenTimedTasks(new Timer());
            //设置一个每五分钟调用的定时任务
            timingUtil.startFiveMinutesTasks(MapFile);
            //设置一个每5s调用的定时任务
            timingUtil.startFiveSecondsTasks();

            //群老婆列表
            LinkedHashMap<Long, Long> map = FileUtil.readMap(MapFile);
            //设置群老婆Map;
            if (map != null && !map.isEmpty()) {
                instance.setMarryMap(map);
            }
            //获取配置文件
            File ConfigFile = resolveConfigFile("miaomiao.json");
            if (!ConfigFile.exists()) {
                ConfigFile.createNewFile();
            }
            File ServerFile = resolveConfigFile("serverQuery.json");
            if (!ServerFile.exists()) {
                ServerFile.createNewFile();
            }
            //设置配置文件
            instance.setResolveConfigFile(ConfigFile);
            instance.setResolveConfigMapFile(ServerFile);
            //读取地图订阅配置
            String jsonStr = new String(Files.readAllBytes(ServerFile.toPath()), StandardCharsets.UTF_8);
            //读取系统配置
            String jsonStr2 = new String(Files.readAllBytes(ConfigFile.toPath()), StandardCharsets.UTF_8);
            //获取玩家地图订阅数据
            List<ServerOrderEntry> serverOrderEntries = new ArrayList<>();
            JSONArray jsonArray = JSON.parseArray(jsonStr);
            if (jsonArray != null && !jsonArray.isEmpty()){
                for (int i = 0; i < jsonArray.size(); i++) {
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    ServerOrderEntry serverOrderEntry = JSON.parseObject(jsonObject.toJSONString(), ServerOrderEntry.class);
                    if (serverOrderEntry !=null){
                        serverOrderEntries.add(serverOrderEntry);
                    }
                }
            }
            instance.setServerOrderEntryList(serverOrderEntries);

            //初始化系统配置
            JSONObject configJsonObject = JSON.parseObject(jsonStr2);
            //获取所有的权限群
            List<Integer> Groups = configJsonObject.getList("权限群列表", Integer.class);
            instance.setGroups(Groups);
            //获取查询服务器列表
            Map<String,String> addressesMap = configJsonObject.getObject("服务器列表",Map.class);
            instance.setAddresses(addressesMap);
            //获取需要检测发言的群号
            List<Integer> prohibitGroup = configJsonObject.getList("检测群列表", Integer.class);
            instance.setProhibitGroup(prohibitGroup);
            //获取插件管理人员
            List<Long> manager = configJsonObject.getList("插件管理人员", Long.class);
            instance.setManager(manager);
            //设置百度图片检测配置
            instance.setBaiDuEntry(BaiDuEntry.builder().setApiKey("SGdRoH6go1LS6Yv8eyxoWpv7")
                    .setSecretKey("j3YpheLoPnlEfVlKfQlSmHo6zwQ8Csj9").build());
            //初始化百度Token
            String accessToken = HttpUtil.getAccessToken("SGdRoH6go1LS6Yv8eyxoWpv7", "j3YpheLoPnlEfVlKfQlSmHo6zwQ8Csj9");
            if (accessToken != null) {
                instance.getBaiDuEntry().setAccessToken(accessToken);
            }
            //初始化内容检测
            instance.setOpenProhibit(true);
            System.out.println("初始化成功...");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}